<?php
namespace app\api\model;


use think\Model;

class Userapp extends Model
{
    /**
     * 获取用户APP并格式化
     * @param $shopid
     * @return array
     */
    public function getapps($shopid){
        $res=$this->where(["shopid"=>$shopid])->select();
        $data=[];
        foreach ($res as $k=>$v){
            $data[]=[
                "appid"=>$v["appid"],
                "status"=>$v["status"],
                "intime"=>time()<$v["endtime"]?true:false
            ];
        }
        return $data;
    }


    /**
     * 购买一个APP记录
     * @param $userid
     * @return bool
     */
    public function buyapp($userid){
        $oriinfo=$this->where("appid",request()->post("appid"))->where(["shopid"=>request()->post("shopid")])->find();
        if($oriinfo){
            $expiretime=strtotime("+".request()->post("year")." year",time()>$oriinfo["endtime"]?time():$oriinfo["endtime"]);
            if(($expiretime>=pow(2,31)-1)||$expiretime<100){
                return false;
            }
            //        第一步:向Appbuyorder中记录
            $ordermodel=new Appbuyorder();
            $ordermodel->data([
                "shopid"=>request()->post("shopid"),
                "userid"=>$userid,
                "createtime"=>time(),
                "appid"=>request()->post("appid"),
                "appname"=>"",
                "expiretime"=>strtotime("+".request()->post("year")." year",time()>$oriinfo["endtime"]?time():$oriinfo["endtime"]),
                "orderstatus"=>1,
                "price"=>request()->post("price")
            ]);
            $res1=$ordermodel->isUpdate(false)->save();
            //        第二步:向UserApp表中记录
            $res2=$this->isUpdate(true,["id"=>$oriinfo["id"]])->save([
                "appid"=>request()->post("appid"),
                "shopid"=>request()->post("shopid"),
                "status"=>1,
                "endtime"=>strtotime("+".request()->post("year")." year",time()>$oriinfo["endtime"]?time():$oriinfo["endtime"])
            ]);
        }else{
            //        第一步:向Appbuyorder中记录
            $ordermodel=new Appbuyorder();
            $ordermodel->data([
                "shopid"=>request()->post("shopid"),
                "userid"=>$userid,
                "createtime"=>time(),
                "appid"=>request()->post("appid"),
                "appname"=>"",
                "expiretime"=>strtotime("+".request()->post("year")." year",time()),
                "orderstatus"=>1,
                "price"=>request()->post("price")
            ]);
            $res1=$ordermodel->save();
            //        第二步:向UserApp表中记录
            $this->data([
                "appid"=>request()->post("appid"),
                "shopid"=>request()->post("shopid"),
                "status"=>1,
                "starttime"=>time(),
                "endtime"=>strtotime("+".request()->post("year")." year",time())
            ]);
            $res2=$this->save();
        }

        return $res1&&$res2;//返回记录结果
    }

    /**
     * 使用POS购买一个APP
     */
    public function posbuyapp($userid,$appid,$year,$price){
        $shopid=model("manager")->where("id",$userid)->column("shopid")[0];
        if(!$shopid){
            return false;
        }
        $oriinfo=$this->where("appid",$appid)->where(["shopid"=>$shopid])->find();
        if($oriinfo){
            $expiretime=strtotime("+".$year." year",time()>$oriinfo["endtime"]?time():$oriinfo["endtime"]);
            if(($expiretime>=pow(2,31)-1)||$expiretime<100){
                return false;
            }
            //        第一步:向Appbuyorder中记录
            $ordermodel=new Appbuyorder();
            $ordermodel->data([
                "shopid"=>$shopid,
                "userid"=>$userid,
                "createtime"=>time(),
                "appid"=>$appid,
                "appname"=>"",
                "expiretime"=>strtotime("+".$year." year",time()>$oriinfo["endtime"]?time():$oriinfo["endtime"]),
                "orderstatus"=>1,
                "price"=>$price
            ]);
            $res1=$ordermodel->isUpdate(false)->save();
            //        第二步:向UserApp表中记录
            $res2=$this->isUpdate(true,["id"=>$oriinfo["id"]])->save([
                "appid"=>$appid,
                "shopid"=>$shopid,
                "status"=>1,
                "endtime"=>strtotime("+".$year." year",time()>$oriinfo["endtime"]?time():$oriinfo["endtime"])
            ]);
        }else{
            //        第一步:向Appbuyorder中记录
            $ordermodel=new Appbuyorder();
            $ordermodel->data([
                "shopid"=>$shopid,
                "userid"=>$userid,
                "createtime"=>time(),
                "appid"=>$appid,
                "appname"=>"",
                "expiretime"=>strtotime("+".$year." year",time()),
                "orderstatus"=>1,
                "price"=>$price
            ]);
            $res1=$ordermodel->save();
            //        第二步:向UserApp表中记录
            $this->data([
                "appid"=>$appid,
                "shopid"=>$shopid,
                "status"=>1,
                "starttime"=>time(),
                "endtime"=>strtotime("+".$year." year",time())
            ]);
            $res2=$this->save();
        }

        return $res1&&$res2;//返回记录结果
    }


    /**
     *  获取POS用户APP列表
     */
    public function getuserapp($userid){
        $shopid=model("manager")->where("id",$userid)->column("shopid")[0];
        if(!$shopid){
            return false;
        }
        return $this->where("y_userapp.shopid",$shopid)->where("y_userapp.endtime>".time())
            ->where("status",1)
            ->join("y_applist","y_userapp.appid=y_applist.id","left")
            ->join("y_appdetail","y_applist.id=y_appdetail.pid","left")
            ->field("y_userapp.appid,y_applist.appname,y_applist.iconurl,y_appdetail.*")
            ->select();
    }

}